package com.motu.monstercity.server.game.logichandler;

import com.motu.monstercity.module.common.network.RequestParam;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameShop.CSGameShopBuyRequest;
import com.motu.monstercity.protocol.CsGameShop.CSGameShopBuyResponse;
import com.motu.monstercity.protocol.CsGameShop.CSGameShopMainInfoRequest;
import com.motu.monstercity.protocol.CsGameShop.CSGameShopMainInfoResponse;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.act.Act;
import com.motu.monstercity.server.game.commondata.act.ActConstant;
import com.motu.monstercity.server.game.commondata.act.ActConvention;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.commondata.shop.Shop;
import com.motu.monstercity.server.game.commondata.shop.ShopConstant;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.log.ThinkingDataManager;
import com.motu.monstercity.server.game.logic.*;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.toolset.ErrorWord;

import java.util.List;

public class ShopHandler {
    // CMD_GAME_SHOP_MAIN_INFO = 71601; // 玩家的购买记录
    public static Object CMD_GAME_SHOP_MAIN_INFO(RequestParam clientParam) throws Exception {
        CSGameShopMainInfoRequest request = CSGameShopMainInfoRequest.parseFrom(clientParam.getData());
        CSGameShopMainInfoResponse.Builder response = CSGameShopMainInfoResponse.newBuilder();
        long userId = clientParam.getUserId();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        List<UserShopLimit> list = ShopManager.getUserShopLimitList(userId);
        for (UserShopLimit userShopLimit : list) {
            ProtoDataUtils.updatePBUserData(pbUserData, userShopLimit, true);
        }

        return response;
    }

    // CMD_GAME_SHOP_BUY = 71602; // 购买
    public static Object CMD_GAME_SHOP_BUY(RequestParam clientParam) throws Exception {
        CSGameShopBuyRequest request = CSGameShopBuyRequest.parseFrom(clientParam.getData());
        CSGameShopBuyResponse.Builder response = CSGameShopBuyResponse.newBuilder();
        long userId = clientParam.getUserId();

        int shopId = request.getShopId();
        int num = request.getNum();
        if (num < 1 || num > 999) {
            return ErrorWord.BAD_PARAM;
        }
        Shop shop = ShopManager.getShop(shopId);
        if (shop == null) {
            return ErrorWord.NOT_DATA;
        }



        UserInfo userInfo = GameUser.getUserInfo(userId);
        UserAffair userAffair = GameUser.getUserAffair(userId);
        UserStage userStage = FightManager.getUserStage(userId);
        if (shop.getModuleId() > 0 && !SystemManager.checkModuleOpen(userInfo, userAffair, userStage, shop.getModuleId())) {
            return ErrorWord.WRONG_STATUS;// 模块未解锁
        }

        if (!ShopManager.checkShopUnlock(userInfo, shop)) {
            return ErrorWord.WRONG_STATUS;// 该商品尚未解锁,不能购买
        }
        switch (shop.getShopType()) {
            case ShopConstant.SHOP_TYPE_UNION -> {
                if (!UnionManager.isShopBuyBox(userInfo, shop)) {
                    return ErrorWord.WRONG_STATUS;
                }
                break;
            }
            case ShopConstant.SHOP_TYPE_201 -> {
                if (!ActConventionManager.isShopBuyBox(userInfo, shop.getSubShopType())) {
                    return ErrorWord.WRONG_STATUS;
                }
                break;
            }
            case ShopConstant.SHOP_TYPE_301 -> {
                if (!ActConventionManager.isShopBuyItem(userInfo, shop.getSubShopType())){
                    return ErrorWord.WRONG_STATUS;
                }
                break;
            }
            case ShopConstant.SHOP_TYPE_UNION_SCUFFLE -> {
                if (!UnionScuffleManager.isShopBuyBox(userInfo, shop)) {
                    return ErrorWord.WRONG_STATUS;
                }
                break;
            }
        }

        List<RewardItem> costItem = CommonUtils.takeReawrdItemFromStr(shop.getPrice());
        CommonUtils.multipleItemList(costItem, num);//数量 x N倍
        if (shop.getIsFree() == 0 && !BagManager.checkNeedItemNum(userInfo, costItem)) {// 非免费商品
            return ErrorWord.ITEM_LACK;// 道具不足
        }

        UserShopLimit userShopLimit = ShopManager.getUserShopLimit(userId, shop.getShopType());
        if (!ShopManager.checkShopLimit(userShopLimit, shop, num)) {
            return ErrorWord.ALREADY_MAX;// 次数上限
        }

        // 执行逻辑
        PBUserData.Builder pbUserData = response.getUserBuilder();
        if (shop.getIsFree() == 0) {
            RewardManager.subReward(userInfo, costItem, pbUserData, LogType.SHOP_BUY, shopId);
        }
        List<RewardItem> rewardItemList = CommonUtils.takeReawrdItemFromStr(shop.getReward());
        CommonUtils.multipleItemList(rewardItemList, num);//数量 x N倍
        RewardManager.addReward(userInfo, rewardItemList, pbUserData, LogType.SHOP_BUY, shopId);
        ShopManager.updateUserShopLimit(userId, userShopLimit, shop, num, pbUserData);// 更新限购记录数据,并下发给客户端

        if (shop.getShopType() == ShopConstant.SHOP_TYPE_7) {
            MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_47, 1);// 地下矿洞商店购买[1]次商品
        }

        // 上报数数
        RewardItem rewardItem = rewardItemList.get(0);
        if (shop.getIsFree() == 0) {// 非免费礼包
            RewardItem costItem22 = costItem.get(0);
            ThinkingDataManager.pushUserShopBuyEvent(userInfo, rewardItem.getItemId(), rewardItem.getItemNum(), costItem22.getItemId(), costItem22.getItemNum(), shop.getShopType());
        } else {// 免费
            ThinkingDataManager.pushUserShopBuyEvent(userInfo, rewardItem.getItemId(), rewardItem.getItemNum(), 0, 0, shop.getShopType());
        }

        return response;
    }

    /**
     * 是否可在商店购买道具
     * 1. 整个活动期
     * @param userInfo
     * @param subShopType
     * @return
     */
    public static boolean isShopBuyItem(UserInfo userInfo, int subShopType){
        Act act = ActManager.getActByType(ActConstant.ACT_TYPE_1001, userInfo.getServerId());
        if (act == null || !act.isCloseTime()) {// 整个活动期
            return false;
        }
        int groupId = Integer.parseInt(act.getParam());
        ActConvention actConvention = ActConventionManager.getActConvention(groupId);
        if (actConvention == null || subShopType != actConvention.getBoxShop()) { // 不是本期的可购买的道具类型
            return false;
        }

        return true;
    }
}
